package com.workstudy.web.controller.workstudy;

import java.util.List;

import com.workstudy.common.core.domain.entity.SysRole;
import com.workstudy.system.service.ISysRoleService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.workstudy.common.annotation.Log;
import com.workstudy.common.enums.BusinessType;
import com.workstudy.workstudy.domain.WorkstudyOpentimeset;
import com.workstudy.workstudy.service.IWorkstudyOpentimesetService;
import com.workstudy.common.core.controller.BaseController;
import com.workstudy.common.core.domain.AjaxResult;
import com.workstudy.common.utils.poi.ExcelUtil;
import com.workstudy.common.core.page.TableDataInfo;

/**
 * 系统开放时间设置Controller
 * 
 * @author ws
 * @date 2021-03-28
 */
@Controller
@RequestMapping("/workstudy/opentimeset")
public class WorkstudyOpentimesetController extends BaseController
{
    private String prefix = "workstudy/opentimeset";

    @Autowired
    private ISysRoleService roleService;

    @Autowired
    private IWorkstudyOpentimesetService workstudyOpentimesetService;

    @RequiresPermissions("workstudy:opentimeset:view")
    @GetMapping()
    public String opentimeset()
    {
        return prefix + "/opentimeset";
    }

    /**
     * 查询系统开放时间设置列表
     */
    @RequiresPermissions("workstudy:opentimeset:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(WorkstudyOpentimeset workstudyOpentimeset)
    {
        startPage();
        List<WorkstudyOpentimeset> list = workstudyOpentimesetService.selectWorkstudyOpentimesetList(workstudyOpentimeset);
        return getDataTable(list);
    }

    /**
     * 导出系统开放时间设置列表
     */
    @RequiresPermissions("workstudy:opentimeset:export")
    @Log(title = "系统开放时间设置", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(WorkstudyOpentimeset workstudyOpentimeset)
    {
        List<WorkstudyOpentimeset> list = workstudyOpentimesetService.selectWorkstudyOpentimesetList(workstudyOpentimeset);
        ExcelUtil<WorkstudyOpentimeset> util = new ExcelUtil<WorkstudyOpentimeset>(WorkstudyOpentimeset.class);
        return util.exportExcel(list, "opentimeset");
    }

    /**
     * 新增系统开放时间设置
     */
    @GetMapping("/add")
    public String add(ModelMap mmap)
    {
        List<SysRole> roleList = roleService.selectRoleAll();
        mmap.put("roleList", roleList);
        return prefix + "/add";
    }

    /**
     * 新增保存系统开放时间设置
     */
    @RequiresPermissions("workstudy:opentimeset:add")
    @Log(title = "系统开放时间设置", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(WorkstudyOpentimeset workstudyOpentimeset)
    {
        if (workstudyOpentimesetService.selectWorkstudyOpentimesetByroleId(workstudyOpentimeset.getRoleId())!=null)
        {
            return error("此角色已设置开放时间!");
        }
        return toAjax(workstudyOpentimesetService.insertWorkstudyOpentimeset(workstudyOpentimeset));
    }

    /**
     * 修改系统开放时间设置
     */
    @GetMapping("/edit/{opentimeSetId}")
    public String edit(@PathVariable("opentimeSetId") Long opentimeSetId, ModelMap mmap)
    {
        List<SysRole> roleList = roleService.selectRoleAll();
        mmap.put("roleList", roleList);
        WorkstudyOpentimeset workstudyOpentimeset = workstudyOpentimesetService.selectWorkstudyOpentimesetById(opentimeSetId);
        mmap.put("workstudyOpentimeset", workstudyOpentimeset);
        return prefix + "/edit";
    }

    /**
     * 修改保存系统开放时间设置
     */
    @RequiresPermissions("workstudy:opentimeset:edit")
    @Log(title = "系统开放时间设置", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(WorkstudyOpentimeset workstudyOpentimeset)
    {
//        if (workstudyOpentimesetService.selectWorkstudyOpentimesetByroleId(workstudyOpentimeset.getRoleId())!=null)
//        {
//            return error("此角色已设置开放时间!");
//        }
        return toAjax(workstudyOpentimesetService.updateWorkstudyOpentimeset(workstudyOpentimeset));
    }

    /**
     * 删除系统开放时间设置
     */
    @RequiresPermissions("workstudy:opentimeset:remove")
    @Log(title = "系统开放时间设置", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(workstudyOpentimesetService.deleteWorkstudyOpentimesetByIds(ids));
    }
}
